// Als Fehlerklasse für die Lagerhaltung, LOL ;-) import java.util.prefs.BackingStoreException; // import java.util.prefs.*; public class AufgabeWaren26 { // public static int foobar = 0; public static Helper h = new Helper(); /* Deklaration weiterer Klassenvariablen */ private static String[] waren = new String[]{ "Ayran", "Schokoriegel", "Stifte", "Briefmarken", "Kaugummis", "Gummibärchen" }; private static int[] lager = new int[]{ 99, 70, 53, 490, 30, 10 }; private static double[] preise = new double[]{ 1.99, 1.30, 0.99, 0.85, 2.49, 3.00 }; public static void main(String[] arg) { try { h.dblIndent = 8; h.intIndent = 5; h.stringIndent = 15; h.currSub = 100; // printLager(); menu_main(); } catch (Throwable t){ System.err.print("Irgendwas ist passiert"); } } public static double verkauf(int wareidx ,int anzahl) throws BackingStoreException { if ( anzahl > lager[wareidx] ) { throw new BackingStoreException("Ware nicht in der gefragten Anzahl vorhanden\n"); } // Lagerbestand verringern lager[wareidx] -= anzahl; // Rückgabe Preis return preise[wareidx] * anzahl; } public static void menu_verkauf() throws Throwable { int ware = 0, anzahl = 0, cnt = 0; Quittung q = new Quittung(); while ( true ) { h.print("\n === Verkauf ===\n"); // Warenliste for ( cnt = 0; cnt < waren.length; cnt++ ) { h.print( (cnt+1) + ". " + waren[cnt] + "\n"); } // Option zum Beenden h.print( 0 + ". " + "Beenden" + "\n"); ware = Read.getInt("Ware: "); // Verkauf beenden if ( ware == 0 ) { // Kassenzettel ausgeben h.print( q.output() ); return; // Eingabe sanitizing // } else if ( ware >= waren.length || ware < 0 ) { // System.err.print("Ware existiert nicht\n"); // Verkauf durchführen } else { try { anzahl = Read.getInt("Anzahl: "); q.posten( anzahl, waren[ware - 1], verkauf( ware - 1 , anzahl) ); printLager(waren[ware - 1]); } catch (Exception foobar) {} } } } public static void menu_main() throws Throwable { int choice = 0; while ( true ) { try { h.print("\n\n === Menu ===\n" + "1. Ware Verkaufen\n" + "2. Lagerbestand anzeigen\n" + "9. Programm Verlassen\n"); choice = Read.getInt("Auswahl: "); if ( choice == 1 ) { menu_verkauf(); } else if ( choice == 2 ) { printLager(); } else if ( choice == 9 ) { return; // Funktion verlassen } } catch (Throwable t) { System.err.print("Untermenü abgebrochen\n"); } } } public static void printLager() { int n; double einzelWert = 0.0; for ( n = 0; n < waren.length; n++ ) { einzelWert = lager[n] * preise[n]; h.print( h.indent( waren[n] )+ ": " + h.indent( lager[n] )+ " " + h.indent(preise[n] )+ " Euro " + h.indent(einzelWert) + " Euro\n" ); } h.print( h.indent("Lager Gesamt: ") + h.indent(25, gesamtWert() ) + " Euro" ); } /* zweite printLager-Funktion */ public static void printLager(String ware) throws Exception { for (int i = 0; i < waren.length; i++) { if (waren[i] == (ware)) { double einzelWert = lager[i] * preise[i]; h.print( h.indent( waren[i]) + ": " + h.indent( lager[i]) + " " + h.indent( preise[i]) + " Euro " + h.indent(einzelWert) + " Euro\n"); return; // Sobald die Ware gefunden wurde, wird die Funktion beendet } } // Wenn die Ware nicht gefunden wurde, wird eine Fehlermeldung ausgegeben // h.print("Ware '" + ware + "' nicht gefunden.\n"); Exception sdw = new Exception(); throw sdw; } /* Funktion gesamtWert */ public static double gesamtWert() { double gesamt = 0.0; for (int n = 0; n < lager.length; n++) { gesamt += lager[n] * preise[n]; } return gesamt; } }